import { _decorator, Node, instantiate, Widget, UITransform, v3 } from "cc";
import { NATIVE_AD_STYLE, NATIVE_AD_TYPE } from "../GlobalData";
import { ResUtils } from "../ResUtils";
import { NativeAdView } from "./NativeAdView";

const { ccclass } = _decorator;

@ccclass
export class FinishNativeAd {
    private adId: any = null;
    private nativeAd: any = null;
    private nativeAdView: Node = null;
    private nativeAdViewCtrl: NativeAdView = null;
    private nativeBannerAdView: Node = null;
    private nativeBannerAdViewCtrl: NativeAdView = null;
    private style: any = NATIVE_AD_STYLE.BANNER_AD;
    public viewAdBtn: Node = null;
    public bgBtn: Node = null;
    private count: number = 0;
    private initCallBack: Function = null;

    constructor() {
        //this.autoUpdateNativeAdData();
    }

    public initNativeStyle(style: any = null) {
        this.style = style;
    }

    public addNodeToParent(node: Node) {
        this.createNativeAdView(node);
        this.createNativeBannerAdView(node);
    }

    private initNativeAd(callBack: Function = null) {
        this.initCallBack = callBack;
        if (!!this.nativeAd) {
            console.log('FinishNativeAd--initNativeAd');
            HeyGameAdSDK.GameAdSDK.SDK.onNativeDestroy(this.nativeAd);
            this.nativeAd = null;
            this.adId = null;
        }
        this.nativeAd = HeyGameAdSDK.GameAdSDK.SDK.createNativeAd(NATIVE_AD_TYPE.FINISH, this.onLoadNativeDataFinish.bind(this));
    }

    private onLoadNativeDataFinish(adData: any) {
        if (adData) {
            this.nativeAdViewCtrl && this.nativeAdViewCtrl.initView(adData);
            this.nativeBannerAdViewCtrl && this.nativeBannerAdViewCtrl.initView(adData);
            this.adId = adData.adId;
            this.initCallBack && this.initCallBack(true);
        } else {
            this.hideNativeAd();
            this.initCallBack && this.initCallBack(false);
        }
    }

    private autoUpdateNativeAdData() {
        setInterval(this.initNativeAd.bind(this), (HeyGameAdSDK.GameAdSDK.SDK.delayTime + 2) * 1000);
    }

    private createNativeAdView(node: Node) {
        if (!this.nativeAdView) {
            ResUtils.loadPrefab('HeyGameSDK/native_ad_prefab', (prefab) => {
                if (prefab) {
                    this.nativeAdView = instantiate(prefab);
                    this.nativeAdViewCtrl = this.nativeAdView.getComponent(NativeAdView);
                    this.nativeAdViewCtrl.adClickCallBack = this.onNativeAdClick.bind(this);
                    this.nativeAdViewCtrl.adCloseCallBack = this.onNativeAdClose.bind(this);
                    node.addChild(this.nativeAdView);
                    if (!!this.nativeWidget) {
                        this.setWidget(this.nativeAdView, this.nativeWidget);
                        // this.nativeWidget = null;
                    }
                    this.nativeAdView.active = false;
                    this.count++;
                    this.onNativeNodeCreateFinish();
                }
            });
        }
    }

    private onNativeNodeCreateFinish() {
        if (this.count >= 2) {
            if (HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame) {
                setTimeout(() => {
                    this.initNativeAd();
                }, 5 * 1000);
            } else {
                this.initNativeAd();
            }
            // this.autoUpdateNativeAdData();
        }
    }

    private createNativeBannerAdView(node: Node) {
        if (!this.nativeBannerAdView) {
            ResUtils.loadPrefab('HeyGameSDK/native_banner_ad_prefab', (prefab) => {
                if (prefab) {
                    this.nativeBannerAdView = instantiate(prefab);
                    this.nativeBannerAdViewCtrl = this.nativeBannerAdView.getComponent(NativeAdView);
                    this.nativeBannerAdViewCtrl.adClickCallBack = this.onNativeAdClick.bind(this);
                    this.nativeBannerAdViewCtrl.adCloseCallBack = this.onNativeAdClose.bind(this);
                    node.addChild(this.nativeBannerAdView);
                    if (!!this.bannerWidget) {
                        this.setWidget(this.nativeBannerAdView, this.bannerWidget);
                        this.bannerWidget = null;
                    }
                    this.nativeBannerAdView.active = false;
                    this.count++;
                    this.onNativeNodeCreateFinish();
                }
            });
        }
    }

    public onNativeAdClick() {
        if (this.nativeAd) {
            HeyGameAdSDK.GameAdSDK.SDK.onNativeAdClick(this.nativeAd, this.adId, (status) => {
                if (status) {
                    if (HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame) {
                        setTimeout(() => {
                            this.initNativeAd(() => {
                                this.showNativeAd();
                            });
                        }, 4 * 1000);
                    } else {
                        this.initNativeAd(() => {
                            this.showNativeAd();
                        });
                    }
                }
            });
        }
    }

    private onNativeAdClose() {
        if (this.nativeAd) {
            HeyGameAdSDK.GameAdSDK.SDK.onNativeAdClose(this.nativeAd, this.adId, (status) => {
                if (status) this.initNativeAd();
            });
        }
        if (!!this.viewAdBtn) {
            this.viewAdBtn.active = false;
        }
        if (!!this.bgBtn) {
            this.bgBtn.active = false;
        }
        HeyGameAdSDK.GameAdSDK.SDK.showBannerAd(106);
    }

    public showNativeAd(style: any = null, widget?: any) {
        if (style != null)
            this.style = style;
        if (this.adId == null) {
            this.initNativeAd((status) => {
                if (status) {
                    this.showNativeAd();
                } else {
                    HeyGameAdSDK.GameAdSDK.SDK.showBannerAd(107);
                }
            });
            return;
        }
        HeyGameAdSDK.GameAdSDK.SDK.hideBannerAd();
        if (!!this.bgBtn) {
            this.bgBtn.active = true;
        }
        if (!!this.viewAdBtn) {
            this.viewAdBtn.active = true;
        }
        switch (this.style) {
            case NATIVE_AD_STYLE.BIG_AD:
                if (this.nativeBannerAdView) {
                    this.nativeBannerAdView.active = false;
                }
                if (!!widget) {
                    this.nativeWidget = widget;
                }
                if (this.nativeAdView) {
                    if (!!widget) {
                        this.setWidget(this.nativeAdView, widget);
                        // this.nativeWidget = null;
                    } else if (!!this.nativeWidget) {
                        this.setWidget(this.nativeAdView, this.nativeWidget);
                        // this.nativeWidget = null;
                    }
                    this.nativeAdView.active = true;
                }
                break;
            case NATIVE_AD_STYLE.BANNER_AD:
                if (this.nativeAdView) {
                    this.nativeAdView.active = false;
                }
                if (!!widget) {
                    this.bannerWidget = widget;
                }
                if (this.nativeBannerAdView) {
                    if (!!widget) {
                        this.setWidget(this.nativeBannerAdView, widget);
                        this.bannerWidget = null;
                    } else if (!!this.bannerWidget) {
                        this.setWidget(this.nativeBannerAdView, this.bannerWidget);
                        this.bannerWidget = null;
                    }
                    this.nativeBannerAdView.active = true;
                }
                break;
        }
    }
    protected bannerWidget: any;
    protected nativeWidget: any;
    //设置布局组件
    protected setWidget(node: Node, widget: any, targetNode?: Node) {
        //尺寸
        let trans = node.getComponent(UITransform);
        if (undefined !== widget.width) {
            trans.width = widget.width;
        }
        if (undefined !== widget.height) {
            trans.height = widget.height;
        }
        let p = v3();
        let dirty: boolean = false;
        if (undefined !== widget.x) {
            p.x = widget.x;
            dirty = true;
        } else {
            p.x = node.position.x;
        }
        if (undefined !== widget.y) {
            p.y = widget.y;
            dirty = true;
        } else {
            p.y = node.position.y;
        }
        if (dirty) {
            p.z = node.position.z;
            node.setPosition(p);
        }

        //缩放
        p.x = p.y = p.z = 1;
        if (undefined !== widget.scale) {
            p.x = widget.scale.x;
            p.y = widget.scale.y;
        }
        node.setScale(p);

        let wg = node.getComponent(Widget);
        if (!wg) {
            wg = node.addComponent(Widget);
        }
        wg.isAbsoluteBottom = true;
        wg.isAbsoluteLeft = true;
        wg.isAbsoluteRight = true;
        wg.isAbsoluteTop = true;
        wg.isAbsoluteHorizontalCenter = true;
        wg.isAbsoluteVerticalCenter = true;
        if (!widget) return;
        if (!!targetNode) {
            wg.target = targetNode;
        } else {
            wg.target = node.parent;
        }
        //上
        if (undefined !== widget.top) {
            wg.isAlignTop = true;
            wg.isAbsoluteTop = widget.top > 1;
            wg.top = parseFloat(widget.top);
        } else {
            wg.isAlignTop = false;
        }
        //下
        if (undefined !== widget.bottom) {
            wg.isAlignBottom = true;
            wg.isAbsoluteBottom = widget.bottom > 1;
            wg.bottom = parseFloat(widget.bottom);
        } else {
            wg.isAlignBottom = false;
        }
        //左
        if (undefined !== widget.left) {
            wg.isAlignLeft = true;
            wg.isAbsoluteLeft = widget.left > 1;
            wg.left = parseFloat(widget.left);
        } else {
            wg.isAlignLeft = false;
        }
        //右
        if (undefined !== widget.right) {
            wg.isAlignRight = true;
            wg.isAbsoluteRight = widget.right > 1;
            wg.right = parseFloat(widget.right);
        } else {
            wg.isAlignRight = false;
        }

        wg.isAlignHorizontalCenter = !!widget.horizontalCenter;
        wg.isAlignVerticalCenter = !!widget.verticalCenter;
        this.updateWidget(node);
    }
    protected updateWidget(node: Node) {
        let wg = node.getComponent(Widget);
        if (!!wg) {
            wg.updateAlignment();
        }
        for (let i = node.children.length - 1; i >= 0; --i) {
            this.updateWidget(node.children[i]);
        }
    }

    public hideNativeAd() {
        if (this.nativeAdView) {
            this.nativeAdView.active = false;
        }
        if (this.nativeBannerAdView) {
            this.nativeBannerAdView.active = false;
        }
        if (!!this.viewAdBtn) {
            this.viewAdBtn.active = false;
        }
        if (!!this.bgBtn) {
            this.bgBtn.active = false;
        }
    }

}